package com.hex.liyue.usermodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.usermodule.bean.AddressResultBean;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterAddress extends RecyclerView.Adapter<AdapterAddress.MyHolder> {
    private List<AddressResultBean.DataBean> list;

    public void setList(List<AddressResultBean.DataBean> list) {
        this.list = list;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onEditClickListener(int position);

        void onDeleteClickListener(int position);

        void onFirstClickListener(int position);

        void onItemClickListener(int position);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_address, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        AddressResultBean.DataBean dataBean = list.get(position);
        holder.tv_username.setText("收货人:" + dataBean.username);
        holder.tv_mobile.setText(dataBean.mobile);
        holder.tv_address.setText(dataBean.province_name + " " + dataBean.city_name + " " + dataBean.district_name + " " + dataBean.address_detail);
        if (dataBean.is_default.equals("1")) {
            holder.iv_address_default.setImageResource(R.drawable.address_press);
            UserManager.updateAddress(dataBean);
        } else {
            holder.iv_address_default.setImageResource(R.drawable.unchecked);
        }

        holder.ll_edit_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onEditClickListener(position);
            }
        });
        holder.ll_delete_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onDeleteClickListener(position);
            }
        });
        holder.ll_update_address_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onFirstClickListener(position);
            }
        });
        holder.rl_item_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }


    class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_item_box;
        LinearLayout ll_edit_address, ll_delete_address, ll_update_address_default;
        TextView tv_username, tv_mobile, tv_address;
        ImageView iv_address_default;

        public MyHolder(View itemView) {
            super(itemView);
            rl_item_box = (RelativeLayout) itemView.findViewById(R.id.rl_item_box);
            ll_edit_address = (LinearLayout) itemView.findViewById(R.id.ll_edit_address);
            ll_delete_address = (LinearLayout) itemView.findViewById(R.id.ll_delete_address);
            ll_update_address_default = (LinearLayout) itemView.findViewById(R.id.ll_update_address_default);

            tv_username = (TextView) itemView.findViewById(R.id.tv_username);
            tv_mobile = (TextView) itemView.findViewById(R.id.tv_mobile);
            tv_address = (TextView) itemView.findViewById(R.id.tv_address);
            iv_address_default = (ImageView) itemView.findViewById(R.id.iv_address_default);
        }

    }
}
